package com.example.demo.test.lecture.httpstest;



import com.alibaba.csb.sdk.internel.HttpClientHelper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;



public class HttpParameters {
    private Builder builder;

    String getApi() {
        return this.builder.api;
    }

    String getVersion() {
        return this.builder.version;
    }

    String getAccessKey() {
        return this.builder.ak;
    }

    String getSecretkey() {
        return this.builder.sk;
    }

    String getMethod() {
        return this.builder.method;
    }

    String getRequestUrl() {
        return this.builder.requestUrl;
    }

    ContentBody getContentBody() {
        return this.builder.contentBody;
    }

    String getRestfulProtocolVersion() {
        return this.builder.restfulProtocolVersion;
    }

    Map<String, String> getParamsMap() {
        return this.builder.paramsMap;
    }

    Map<String, String> getHeaderParamsMap() {
        return this.builder.headerParamsMap;
    }

    boolean isNonce() {
        return this.builder.nonce;
    }

    boolean isSignContentBody() {
        return this.builder.signContentBody;
    }

    boolean isTimestamp() {
        return this.builder.timestamp;
    }

    boolean isDiagnostic() {
        return this.builder.diagnostic;
    }

    public String getSignImpl() {
        return this.builder.signImpl;
    }

    public String getVerifySignImpl() {
        return this.builder.verifySignImpl;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("requestUrl=").append(this.getRequestUrl());
        sb.append("\n api=").append(this.getApi());
        sb.append("\n version=").append(this.getVersion());
        sb.append("\n method=").append(this.getMethod());
        sb.append("\n accessKey=").append(this.getAccessKey());
        sb.append("\n secretKey=").append("*********");
        sb.append("\n contentBody=").append(this.getContentBody());
        sb.append("\n Nonce=").append(this.isNonce());
        sb.append("\n Timestamp=").append(this.isTimestamp());
        sb.append("\n signImpl=").append(this.getSignImpl());
        sb.append("\n verifySignImpl=").append(this.getVerifySignImpl());
        sb.append("\n isDiagnostic=").append(this.isDiagnostic());
        sb.append("\n params: \n");
        Iterator var2 = this.builder.paramsMap.entrySet().iterator();

        Entry entry;
        while(var2.hasNext()) {
            entry = (Entry)var2.next();
            sb.append((String)entry.getKey()).append("=").append((String)entry.getValue()).append("\n");
        }

        sb.append("\n http header params: \n");
        var2 = this.builder.headerParamsMap.entrySet().iterator();

        while(var2.hasNext()) {
            entry = (Entry)var2.next();
            sb.append((String)entry.getKey()).append("=").append((String)entry.getValue()).append("\n");
        }

        return sb.toString();
    }

    private HttpParameters(Builder builder) {
        this.builder = builder;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public void validate() {
        if (this.getRequestUrl() == null) {
            throw new IllegalArgumentException("Bad httpparameters: null requestUrl!");
        } else if (this.getApi() == null) {
            throw new IllegalArgumentException("Bad httpparameters: null api!");
        } else {
            if (this.getContentBody() != null) {
                if (!"post".equalsIgnoreCase(this.getMethod())) {
                    throw new IllegalArgumentException("Bad httpparameters: method must be \"post\" when contentBody is set!");
                }

                if (this.getParamsMap() != null && this.getParamsMap().size() > 0) {
                }
            }

            this.builder.api = HttpClientHelper.trimWhiteSpaces(this.getApi());
            this.builder.version = HttpClientHelper.trimWhiteSpaces(this.getVersion());
            this.builder.ak = HttpClientHelper.trimWhiteSpaces(this.getAccessKey());
            this.builder.sk = HttpClientHelper.trimWhiteSpaces(this.getSecretkey());
        }
    }

    public static class Builder {
        private String api;
        private String version;
        private String ak;
        private String sk;
        private String restfulProtocolVersion;
        private String method = "GET";
        private ContentBody contentBody = null;
        private String requestUrl;
        private String signImpl;
        private String verifySignImpl;
        private boolean nonce;
        private boolean timestamp = true;
        private boolean signContentBody = false;
        private Map<String, String> paramsMap = new HashMap();
        private Map<String, String> headerParamsMap = new HashMap();
        private boolean diagnostic = false;

        public Builder() {
        }

        public Builder api(String api) {
            this.api = api;
            return this;
        }

        public Builder version(String version) {
            this.version = version;
            return this;
        }

        public Builder accessKey(String ak) {
            this.ak = ak;
            return this;
        }

        public Builder secretKey(String sk) {
            this.sk = sk;
            return this;
        }

        public Builder restfulProtocolVersion(String restfulProtocolVersion) {
            this.restfulProtocolVersion = restfulProtocolVersion;
            return this;
        }

        public Builder method(String method) {
            if (!"get".equalsIgnoreCase(method) && !"post".equalsIgnoreCase(method) && !"cget".equalsIgnoreCase(method) && !"cpost".equalsIgnoreCase(method)) {
                throw new IllegalArgumentException("只支持 'GET', 'CGET' or 'POST', 'CPOST' method类型");
            } else {
                this.method = method;
                return this;
            }
        }

        public Builder timestamp(boolean timestampFlag) {
            this.timestamp = timestampFlag;
            return this;
        }

        public Builder nonce(boolean nonceFlag) {
            this.nonce = nonceFlag;
            return this;
        }

        public Builder requestURL(String url) {
            this.requestUrl = url;
            return this;
        }

        public Builder clearParamsMap() {
            this.paramsMap.clear();
            return this;
        }

        public Builder putParamsMap(String key, String value) {
            this.paramsMap.put(key, value);
            return this;
        }

        public Builder putParamsMapAll(Map<String, String> map) {
            if (map != null) {
                this.paramsMap.putAll(map);
                return this;
            } else {
                throw new IllegalArgumentException("empty map!!");
            }
        }

        public Builder clearHeaderParamsMap() {
            this.headerParamsMap.clear();
            return this;
        }

        public Builder putHeaderParamsMap(String key, String value) {
            this.headerParamsMap.put(key, value);
            return this;
        }

        public Builder putHeaderParamsMapAll(Map<String, String> map) {
            if (map != null) {
                this.headerParamsMap.putAll(map);
                return this;
            } else {
                throw new IllegalArgumentException("empty map!!");
            }
        }

        public Builder contentBody(ContentBody cb) {
            this.contentBody = cb;
            return this;
        }

        public Builder sginContentBody(boolean sign) {
            this.signContentBody = sign;
            return this;
        }

        public Builder signImpl(String signImpl) {
            this.signImpl = signImpl;
            return this;
        }

        public Builder verifySignImpl(String verifySignImpl) {
            this.verifySignImpl = verifySignImpl;
            return this;
        }

        public Builder diagnostic(boolean diagnostic) {
            this.diagnostic = diagnostic;
            return this;
        }

        public HttpParameters build() {
            return new HttpParameters(this);
        }
    }
}
